<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/xiehuan.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/xiehuan.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="Java使用poi动态生成表格头，合并列，合并行">
<meta property="og:type" content="article">
<meta property="og:title" content="poi操作excel">
<meta property="og:url" content="http://yoursite.com/2018/12/11/poi操作excel/index.html">
<meta property="og:site_name" content="xiehuan">
<meta property="og:description" content="Java使用poi动态生成表格头，合并列，合并行">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-12-11T11:16:14.720Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="poi操作excel">
<meta name="twitter:description" content="Java使用poi动态生成表格头，合并列，合并行">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/12/11/poi操作excel/"/>





  <title>poi操作excel | xiehuan</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">xiehuan</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/12/11/poi操作excel/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="xiehuan">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="xiehuan">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">poi操作excel</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-12-11T15:34:53+08:00">
                2018-12-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Java使用poi动态生成表格头，合并列，合并行<br><a id="more"></a><br>公司项目需要生成报表，表头需要动态配置，一级表头，二级表头，三级表头目前已经实现。一二三级表头都是合并列，不需要合并行。<br>新需求： 相邻单元格数据相同，合并行</p>
<p>由于一二三级表头已经生成，即合并行操作已经完成，目前只能在生成好的表头进行操作。</p>
<p>使用的方法：<br>1.取出表格的总列数，循环（i）；<br>2.定义一个mergeCol=0；<br>3.取出需要判断的表头的总行数，循环（j）；<br>4.每一列的每一行去判断是否属于被合并的单元格，如果属于，跳过此次j循环，不进行下面的操作；<br>5.如果不属于被合并的单元格，取出当前(i)(j)的值，进行非空判断，如果为空，跳过本次j循环；<br>6.如果(i)(j)不为空，取出(i)(j+1)位置的单元格值。如果(i)(j+1)为空，对mergeCol进行判断，如果mergeCol&gt;0，则进行单元格合并；<br>7.(i)(j)和(i)(j+1)都不为空，(i)(j)是否与(i)(j+1)相等，如果相等，mergeCol+1，否则对mergeCol进行判断，如果mergeCol&gt;0，则进行单元格合并</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">  * 获取sheet中的合并单元格</span><br><span class="line">  * @param sheet</span><br><span class="line">  * @return</span><br><span class="line">  */</span><br><span class="line"> public static Map getNumMergedRegions(HSSFSheet sheet)&#123;</span><br><span class="line">     Map map=new HashMap(4);</span><br><span class="line">     List&lt;Integer&gt;  firstColumns=new LinkedList&lt;&gt;();</span><br><span class="line">     List&lt;Integer&gt;  lastColumns=new LinkedList&lt;&gt;();</span><br><span class="line">     List&lt;Integer&gt;  firstRows=new LinkedList&lt;&gt;();</span><br><span class="line">     List&lt;Integer&gt;  lastRows=new LinkedList&lt;&gt;();</span><br><span class="line">     //得到所有的合并单元格</span><br><span class="line">     int numMergedRegions = sheet.getNumMergedRegions();</span><br><span class="line">     for (int i=0;i&lt;numMergedRegions;i++)&#123;</span><br><span class="line">         //取出排序为i的合并单元格</span><br><span class="line">         CellRangeAddress mergedRegion = sheet.getMergedRegion(i);</span><br><span class="line">         //起始列</span><br><span class="line">         int firstColumn = mergedRegion.getFirstColumn();</span><br><span class="line">         //结束列</span><br><span class="line">         int lastColumn = mergedRegion.getLastColumn();</span><br><span class="line">         //起始行</span><br><span class="line">         int firstRow = mergedRegion.getFirstRow();</span><br><span class="line">         //结束行</span><br><span class="line">         int lastRow = mergedRegion.getLastRow();</span><br><span class="line"></span><br><span class="line">         firstColumns.add(firstColumn);</span><br><span class="line">         lastColumns.add(lastColumn);</span><br><span class="line">         firstRows.add(firstRow);</span><br><span class="line">         lastRows.add(lastRow);</span><br><span class="line">     &#125;</span><br><span class="line">     map.put(&quot;firstColumns&quot;,firstColumns);</span><br><span class="line">     map.put(&quot;lastColumns&quot;,lastColumns);</span><br><span class="line">     map.put(&quot;firstRows&quot;,firstRows);</span><br><span class="line">     map.put(&quot;lastRows&quot;,lastRows);</span><br><span class="line">     return map;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> /**</span><br><span class="line">  * 合并单元格表头  列合并</span><br><span class="line">  * @param sheet</span><br><span class="line">  */</span><br><span class="line"> public static  void MergedRegion(HSSFSheet sheet,int firstRow,int lastRow)&#123;</span><br><span class="line">     Map numMergedRegions = getNumMergedRegions(sheet);</span><br><span class="line">     //起始列，结束列，起始行，结束行</span><br><span class="line"></span><br><span class="line">     //总列数</span><br><span class="line">     int coloumNum=sheet.getRow(firstRow).getPhysicalNumberOfCells();</span><br><span class="line">     for (int i=0;i&lt;coloumNum;i++)&#123;</span><br><span class="line">         int mergeCol=0;</span><br><span class="line">         for (int j = firstRow; j &lt;lastRow; j++) &#123;</span><br><span class="line">             boolean ins = GeneralPathUtil.isIns(i, j, numMergedRegions);</span><br><span class="line">             if (ins)&#123;</span><br><span class="line">                 System.out.println(&quot;continue&quot;);</span><br><span class="line">                 continue;</span><br><span class="line">             &#125;</span><br><span class="line"></span><br><span class="line">             HSSFCell cell = sheet.getRow(j).getCell(i);</span><br><span class="line">             String replace=&quot;&quot;;</span><br><span class="line">             if (cell==null)&#123;</span><br><span class="line">             //if (cell==null||cell.getStringCellValue()==&quot;&quot;)&#123;</span><br><span class="line">                 continue;</span><br><span class="line">             &#125;else &#123;</span><br><span class="line">                 replace = StrUtil.replace(cell.getStringCellValue(), &quot;\r\n\t&quot;, &quot;&quot;).trim();</span><br><span class="line">             &#125;</span><br><span class="line"></span><br><span class="line">             HSSFCell cell1 = sheet.getRow(j + 1).getCell(i);</span><br><span class="line">             String replace2=&quot;&quot;;</span><br><span class="line">             if (cell1!=null)&#123;</span><br><span class="line">            // if (cell1!=null&amp;&amp;cell1.getStringCellValue()!=&quot;&quot;)&#123;</span><br><span class="line">                 replace2 = StrUtil.replace(cell1.getStringCellValue(), &quot;\r\n\t&quot;, &quot;&quot;).trim();</span><br><span class="line">             &#125;</span><br><span class="line">             System.out.println(&quot;j:&quot;+j+&quot;=replace:&quot;+replace+&quot;,j+1:&quot;+(j+1)+&quot;=replace2:&quot;+replace2);</span><br><span class="line">             if (cell1==null)&#123;</span><br><span class="line">                 if (mergeCol&gt;0)&#123;</span><br><span class="line">                     CellRangeAddress cellRangeAddress = new CellRangeAddress(j-mergeCol, j, i, i);</span><br><span class="line">                     System.out.println(&quot;cell1==null 合并区域&#123;&quot;+(j-mergeCol)+&quot;,&quot;+j+&quot;,&quot;+i+&quot;,&quot;+i+&quot;&#125;&quot;);</span><br><span class="line">                     sheet.addMergedRegion(cellRangeAddress);</span><br><span class="line">                     mergeCol=0;</span><br><span class="line">                 &#125;</span><br><span class="line">             &#125;</span><br><span class="line">             if (replace.equals(replace2))&#123;</span><br><span class="line">                 mergeCol++;</span><br><span class="line">             &#125;else&#123;</span><br><span class="line">                 if (mergeCol&gt;0)&#123;</span><br><span class="line">                     CellRangeAddress cellRangeAddress = new CellRangeAddress(j-mergeCol, j, i, i);</span><br><span class="line">                     System.out.println(&quot;replace!=replace2 合并区域&#123;&quot;+(j-mergeCol)+&quot;,&quot;+j+&quot;,&quot;+i+&quot;,&quot;+i+&quot;&#125;&quot;);</span><br><span class="line">                     sheet.addMergedRegion(cellRangeAddress);</span><br><span class="line">                     mergeCol=0;</span><br><span class="line">                 &#125;</span><br><span class="line">             &#125;</span><br><span class="line"></span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">import java.awt.geom.Point2D;</span><br><span class="line">import java.util.ArrayList;</span><br><span class="line">import java.util.List;</span><br><span class="line">import java.util.Map;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @author :xie</span><br><span class="line"> * Email: 1487471733@qq.com</span><br><span class="line"> * Date: 2018/12/10</span><br><span class="line"> * Time: 18:12</span><br><span class="line"> * Describe:list需要有序，画多边形 的最后一步是第一个点与最后一个点相连接</span><br><span class="line"> */</span><br><span class="line">public class GeneralPathUtil &#123;</span><br><span class="line">    </span><br><span class="line">    public static boolean isIns(double pointX, double pointY, Map map)&#123;</span><br><span class="line">        //&#123;起始行，结束行，起始列，结束列&#125;</span><br><span class="line">        List&lt;Integer&gt;  firstRows=(List&lt;Integer&gt;) map.get(&quot;firstRows&quot;);</span><br><span class="line">        List&lt;Integer&gt;  lastRows=(List&lt;Integer&gt;) map.get(&quot;lastRows&quot;);</span><br><span class="line">        List&lt;Integer&gt;  firstColumns= (List&lt;Integer&gt;) map.get(&quot;firstColumns&quot;);</span><br><span class="line">        List&lt;Integer&gt;  lastColumns=(List&lt;Integer&gt;) map.get(&quot;lastColumns&quot;);</span><br><span class="line"></span><br><span class="line">        for (int i=0;i&lt;firstColumns.size();i++)&#123;</span><br><span class="line"></span><br><span class="line">            //如何判断是否是一条线  起始行==结束行</span><br><span class="line">            if (firstRows.get(i).equals(lastRows.get(i))&amp;&amp;Double.parseDouble(firstRows.get(i).toString())==pointY)&#123;</span><br><span class="line">                if (Double.parseDouble(firstColumns.get(i).toString())&lt;=pointX&amp;&amp;Double.parseDouble(lastColumns.get(i).toString())&gt;=pointX)&#123;</span><br><span class="line">                    return true;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;else&#123;</span><br><span class="line">                double[] Xs=&#123;firstColumns.get(i),lastColumns.get(i),lastColumns.get(i),firstColumns.get(i)&#125;;</span><br><span class="line">                double[] Ys=&#123;firstRows.get(i),firstRows.get(i),lastRows.get(i),lastRows.get(i)&#125;;</span><br><span class="line">                boolean inPolygon = isInPolygon(pointX, pointY, Xs, Ys);</span><br><span class="line">                if (inPolygon)&#123;</span><br><span class="line">                    return true;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        return false;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 判断是否在多边形区域内</span><br><span class="line">     *</span><br><span class="line">     * @param pointX</span><br><span class="line">     *            要判断的点的横坐标</span><br><span class="line">     * @param pointY</span><br><span class="line">     *            要判断的点的纵坐标</span><br><span class="line">     * @param Xs</span><br><span class="line">     *            区域各顶点的横坐标数组</span><br><span class="line">     * @param Ys</span><br><span class="line">     *            区域各顶点的纵坐标数组</span><br><span class="line">     * @return</span><br><span class="line">     */</span><br><span class="line">    public static boolean isInPolygon(double pointX, double pointY, double[] Xs,</span><br><span class="line">                                      double[] Ys) &#123;</span><br><span class="line">        // 将要判断的横纵坐标组成一个点</span><br><span class="line">        Point2D.Double point = new Point2D.Double(pointX, pointY);</span><br><span class="line">        // 将区域各顶点的横纵坐标放到一个点集合里面</span><br><span class="line">        List&lt;Point2D.Double&gt; pointList = new ArrayList&lt;Point2D.Double&gt;();</span><br><span class="line">        double polygonPoint_x = 0.0, polygonPoint_y = 0.0;</span><br><span class="line">        for (int i = 0; i &lt; Xs.length; i++) &#123;</span><br><span class="line">            polygonPoint_x = Xs[i];</span><br><span class="line">            polygonPoint_y = Ys[i];</span><br><span class="line">            Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);</span><br><span class="line">            pointList.add(polygonPoint);</span><br><span class="line">        &#125;</span><br><span class="line">        return check(point, pointList);</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     * 一个点是否在多边形内</span><br><span class="line">     *</span><br><span class="line">     * @param point</span><br><span class="line">     *            要判断的点的横纵坐标</span><br><span class="line">     * @param polygon</span><br><span class="line">     *            组成的顶点坐标集合</span><br><span class="line">     * @return</span><br><span class="line">     */</span><br><span class="line">    private static boolean check(Point2D.Double point, List&lt;Point2D.Double&gt; polygon) &#123;</span><br><span class="line">        java.awt.geom.GeneralPath peneralPath = new java.awt.geom.GeneralPath();</span><br><span class="line"></span><br><span class="line">        Point2D.Double first = polygon.get(0);</span><br><span class="line">        // 通过移动到指定坐标（以双精度指定），将一个点添加到路径中</span><br><span class="line">        peneralPath.moveTo(first.x, first.y);</span><br><span class="line">        polygon.remove(0);</span><br><span class="line">        for (Point2D.Double d : polygon) &#123;</span><br><span class="line">            // 通过绘制一条从当前坐标到新指定坐标（以双精度指定）的直线，将一个点添加到路径中。</span><br><span class="line">            peneralPath.lineTo(d.x, d.y);</span><br><span class="line">        &#125;</span><br><span class="line">        // 将几何多边形封闭</span><br><span class="line">        peneralPath.lineTo(first.x, first.y);</span><br><span class="line">        peneralPath.closePath();</span><br><span class="line">        // 测试指定的 Point2D 是否在 Shape 的边界内。</span><br><span class="line">        return peneralPath.contains(point);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/12/06/链表/" rel="next" title="链表">
                <i class="fa fa-chevron-left"></i> 链表
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/12/Binary-Search-Tree/" rel="prev" title="Binary Search Tree">
                Binary Search Tree <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">xiehuan</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">27</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xiehuan</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







       <p>Hosted by <a href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a></p>
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
